package com.yt.supplier.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.supplier.models.bo.SupplierInfoSaveBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.yt.supplier.models.service.ISupplierInfoService;
import com.yt.supplier.models.bo.SupplierInfoBO;
import com.yt.supplier.models.dto.SupplierInfoDTO;

import java.util.List;

/**
 * 供应商信息控制器
 *
 * @author lizicong
 * @Date 2025-09-09 11:10:45
 */

@RestController
@RequestMapping("/api/supplierInfo")
@Slf4j
@Api(value = "SupplierInfoApiController",tags = "供应商信息Api接口")
public class SupplierInfoApiController {

    @DubboReference
    private ISupplierInfoService supplierInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SupplierInfoDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SupplierInfoDTO dto = supplierInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "供应商信息列表",notes = "供应商信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<SupplierInfoDTO>> queryPageList(SupplierInfoBO bo){
        Wrapper<SupplierInfoDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<SupplierInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = supplierInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "添加供应商信息",notes = "添加供应商信息")
    @PostMapping(value = "/add")
    public Result insert(@RequestBody SupplierInfoSaveBO saveBO) {
        System.out.println("saveBO "+saveBO);
        boolean flag = supplierInfoService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }
    @ApiOperation(value = "修改供应商信息",notes = "修改供应商信息")
    @PostMapping("/update")
    public Result update(@RequestBody SupplierInfoSaveBO saveBO){
        System.out.println("saveBO "+saveBO);
        SupplierInfoDTO dto = new SupplierInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = supplierInfoService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除供应商信息",notes = "删除供应商信息")
    @ApiImplicitParam(name = "id",value = "供应商信息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        System.out.println("id "+id);
        boolean flag = supplierInfoService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }
    @ApiOperation(value = "供应商列表",notes = "供应商列表")
    @PostMapping("/supplierlist")
    public Result<List<SupplierInfoDTO>> supplierlist(){

            List<SupplierInfoDTO> supplierInfoDTOList = supplierInfoService.selectList(null);
            if (supplierInfoDTOList!= null){
            System.out.println(" supplierInfoDTOList "+supplierInfoDTOList);
            return new Result<>(supplierInfoDTOList);
            } else {
            return new Result<>(CodeEnum.FAIL_100005);
        }
    }
    @ApiOperation(value = "搜索供应商信息", notes = "根据关键词搜索供应商名称/电话")
    @ApiImplicitParam(name = "keyword", value = "搜索关键词（供应商名称/电话）",
            required = true, paramType = "query", dataType = "String")
    @PostMapping("/searchsupplier") // 路径无参数，固定为 /searchsupplier
    public Result<List<SupplierInfoDTO>> searchsupplier(@RequestParam("keyword") String keyword) { // 接收查询参数
        System.out.println("keyword: " + keyword);
        // 注意：这里需要手动拼接 % 实现模糊查询（之前的问题）
        List<SupplierInfoDTO> supplierDTOlist = supplierInfoService.SearchSupplierlist(keyword );
        if (supplierDTOlist == null) {
            return new Result<>(CodeEnum.FAIL_100005);
        }
        System.out.println("supplierDTOlist: " + supplierDTOlist);
        return new Result<>(supplierDTOlist);
    }
}
